package me.muci.A_base.nodeFlow;

import org.junit.Test;

import java.io.*;
import java.util.Objects;

/**
 * ClassName: FileReaderWriterTest <br/>
 * Description: <br/>
 * date: 2021/8/10 11:23<br/>
 *
 * @author muci<br />
 * @description
 * @since JDK 1.8
 */
public class FileReaderWriterTest {

    /**
     * 将文件数据读取到内存
     * 1.实例化File类
     * 2.创建流对象
     * 3.read方法 读入
     * 4.资源关闭
     *
     */
    @Test
    public void read1(){
        FileReader fileReader = null;
        try {
            //路径相当于当前工程(Module)
            //1.实例化File类的对象，指明操作的文件
            File file = new File("src\\main\\resources\\fileReaderWriterTest.txt");
            //2.创建具体的流
            fileReader = new FileReader(file);
            //3.对数据的读入,返回读入的一个字符，若是文件末尾返回-1
            int read = fileReader.read();
            while (read != -1){
                System.out.print((char) read);
                read = fileReader.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭
            try {
                if (!Objects.isNull(fileReader)){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void read2(){
        FileReader fileReader = null;
        try {
            File file = new File("src\\main\\resources\\fileReaderWriterTest.txt");
            fileReader = new FileReader(file);
            //起到缓冲流作用，保存每次读取的字符
            char[] chars = new char[5];
            //返回每次读入字符的个数，到达末尾返回-1
            int len ;
            while (( len = fileReader.read(chars)) != -1){
               /* for (int i = 0; i < len; i++) {
                    System.out.print(chars[i]);
                }*/
                String string = new String(chars,0,len);
                System.out.print(string);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.流的关闭
            try {
                if (!Objects.isNull(fileReader)){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void write(){
        //1.指明写出到的文件
        File file = new File("src\\main\\resources\\fileReaderWriterTest.txt");
        FileWriter writer = null;
        //2.创建具体的流,若文件不存在可直接创建，文件存在的话，默认是不在原有文件上追加(覆盖)
        try {
            writer = new FileWriter(file,true);
            //3.写出操作
            writer.write("I have a dream \n");
            writer.write("you have a dream too");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     *
     */
    @Test
    public void readAndWrite()  {
        File readFile = new File("src\\main\\resources\\read.txt");
        File writeFile = new File("src\\main\\resources\\write.txt");

        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {

            fileReader = new FileReader(readFile);
            fileWriter = new FileWriter(writeFile);

            char[] chars = new char[5];
            int len;//记录每次读取字符的个数
            while ((len = fileReader.read(chars)) != -1){
                fileWriter.write(chars,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileWriter != null){
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileReader != null){
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
